//
//  DownPullView.m
//  OptimumCarCharge
//
//  Created by 童巍 on 16/11/25.
//  Copyright © 2016年 童巍. All rights reserved.
//

#import "DownPullView.h"

static UIWindow *downPullWindow;

@interface DownPullView () <
    UIPickerViewDelegate,
    UIPickerViewDataSource
>
@property(nonatomic, weak)UIView *pullList;
@property(nonatomic, strong)NSArray * dataSource;
@property(nonatomic)NSInteger rows;
@property(nonatomic)BOOL isShowList;
@property(nonatomic ,copy)NSString *sureStr;
@end

@implementation DownPullView
+(instancetype)initWithDownPullImage:(NSString *)imageName frame:(CGRect)frame dataSource:(NSArray<NSString*> *)source showRows:(NSInteger)rows
{
    DownPullView *pullview = [[DownPullView alloc]initWithFrame:frame];
    if (pullview) {
        pullview.isShowList = NO;
        pullview.backgroundColor = [UIColor clearColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:pullview action:@selector(downPullList:)];
        [pullview addGestureRecognizer:tap];
        pullview.userInteractionEnabled = YES;
        pullview.dataSource = source;
        pullview.rows = rows;
        
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.image = [UIImage imageNamed:imageName];
        [pullview addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(pullview.mas_centerY);
            make.right.mas_equalTo(pullview.mas_right).offset(- kWidthOfPt(8));
            make.width.mas_equalTo(kWidthOfPt(50));
            make.height.mas_equalTo(kHeightOfPt(25));
        }];
        
        UILabel *label = [[UILabel alloc]init];
        pullview.label = label;
        if (source.count > 0) {
            label.text = source[0];
        }
        if ([[NSUserDefaults standardUserDefaults]valueForKey:@"PLANTE_Num"]) {
            NSString *planteNum = [[NSUserDefaults standardUserDefaults]valueForKey:@"PLANTE_Num"];
            label.text = [planteNum substringWithRange:NSMakeRange(0, 1)];
        }
        label.font = systemFontOfPX(40);
        label.textColor = [UIColor colorWithHexString:@"#bfcfe5"];
        label.textAlignment = NSTextAlignmentCenter;
        [pullview addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(pullview.mas_left);
            make.top.mas_equalTo(pullview.mas_top);
            make.bottom.mas_equalTo(pullview.mas_bottom);
            make.right.mas_equalTo(imageView.mas_left).offset( - kWidthOfPt(8));
        }];
        
        return pullview ;
    }
    
    return nil;
}

- (void)hiddenPullList
{
    [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        CGRect frame = self.pullList.frame;
        frame.origin.y = kScreenHeight;
        self.pullList.frame = frame;
    } completion:^(BOOL finished) {
        [self.pullList removeFromSuperview];
        [downPullWindow resignKeyWindow];
        downPullWindow = nil;
        _isShowList = NO;
    }];

}

- (void)cancleList
{
    [self hiddenPullList];
}


- (void)sureList
{
    self.label.text = self.sureStr;
    [self hiddenPullList];

}

- (void)downPullList:(UITapGestureRecognizer *)tap
{
    
    if (_isShowList) {
        [self hiddenPullList];
        
    }else{
        UIWindow *window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
        downPullWindow = window;
        UITapGestureRecognizer *windowTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenPullList)];
        [window addGestureRecognizer:windowTap];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 200)];
        view.backgroundColor = [UIColor whiteColor];
        UIButton *cancleButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 40)];
        UIButton *sureButton = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth - 50, 0, 50, 40)];
        [cancleButton setTitle:@"取消" forState:UIControlStateNormal];
        [sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [cancleButton setTitleColor:[UIColor colorWithRed:13/255.0 green:169/255.0 blue:69/255.0 alpha:1] forState:UIControlStateNormal];
        [sureButton setTitleColor:[UIColor colorWithRed:13/255.0 green:169/255.0 blue:69/255.0 alpha:1] forState:UIControlStateNormal];
        [view addSubview:cancleButton];
        [view addSubview:sureButton];
        [cancleButton addTarget:self action:@selector(cancleList) forControlEvents:UIControlEventTouchUpInside];
        [sureButton addTarget:self action:@selector(sureList) forControlEvents:UIControlEventTouchUpInside];
        
        UIPickerView *datePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(50, 0 , kScreenWidth - 100, 200)];
        datePicker.userInteractionEnabled = YES;
        [view addSubview:datePicker];
        datePicker.backgroundColor = [UIColor clearColor];
        self.pullList = view;
        datePicker.delegate = self;
        datePicker.dataSource = self;
        self.sureStr = self.dataSource[0];
        [window addSubview:view];
        [window makeKeyAndVisible];
        [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
                        CGRect frame = view.frame;
                        frame.origin.y = kScreenHeight - 200;
                        view.frame = frame;
                    } completion:nil];

    }
    _isShowList = !_isShowList;
}
#pragma pickviewdelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataSource.count;
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    return self.dataSource[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    self.sureStr = self.dataSource[row];
    

}
@end

